﻿// Allows to transfer data from the component to an object in both direction by using a binder
define([
'Xjs/core/util',
'Xjs/core/binder',
// Ensure the creation of the global Xjs
'Xjs/app'
],
function (util, binder, dom) {
    'use strict';

    var type = 'dataBound', // Use the type registration to avoid recreating the type
    ContentBound = Xjs.getType(type);

    if (ContentBound) { // It has been already loaded
        return ContentBound;
    }

    // Renders the html from a component
    ContentBound = util.define({

        type: type, // Set the type

        initialize: function (conf) {

            conf = conf || {};

            // Initialize the binder
            if (conf.binder) {
                this.binder = binder.create(conf.binder);
            }
        },

        valueFrom: function (data) {

            if (this.binder) { // There was a binder declared

                console.log('Calling valueFrom for type: ' + this.type + ' id: ' + this.id);

                var content = this.binder.get(data);
                this.setContent(content);
            }
        },

        valueTo: function (data) {

            if (this.binder) { // There was a binder declared

                console.log('Calling valueTo for type: ' + this.type + ' id: ' + this.id);

                var content = this.getContent();
                this.binder.set(data, content);
            }
        }
    });

    Xjs.registerType(ContentBound);

    return ContentBound;
});